// Copyright (C) joyinter
// All rights reserved.
// 
// Author : deng xinguo
// Date   : 2005-5-08
#ifndef  __JOYOVERLAPPEDMANAGER_H 
#define  __JOYOVERLAPPEDMANAGER_H

#include "../../client/provider/CommBridgeBase.h"
#include "../../common/base/LockCS.h"
#include "../../common/base/AutoLockCs.h"

#define   MAX_OVERLAPPED_COUNT  64
#pragma once



//##ModelId=44C4B5D200AB
typedef struct OverlappedStruct
{
	SOCKET hSocket;
	LPWSAOVERLAPPED pNewOverlapped;
	LPWSABUF   lpNewBUF;
	DWORD   dwNewBuffersCount;
	LPWSAOVERLAPPED pOldOverlapped;
	LPWSABUF   lpOldBUF;
	DWORD   dwOldBuffersCount;
	struct sockaddr * lpFrom;
	LPINT			lpFromlen;
	LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine;

}OVERLAPPEDSTRUCT, *pOVERLAPPEDSTRUCT;

//##ModelId=44C4B5D200CB
class CJoyOverlappedManager
{
public:
	//##ModelId=44C4B5D200CC
	CJoyOverlappedManager(void);
	//##ModelId=44C4B5D200CE
	~CJoyOverlappedManager(void);
	//##ModelId=44C4B5D200DA
	bool AddOverlapped(SOCKET hSocket,
						LPWSAOVERLAPPED pNewOverlapped,
						LPWSABUF   lpNewBUF,
						DWORD   dwNewBuffersCount,
						LPWSAOVERLAPPED pOldOverlapped,
						LPWSABUF   lpOldBUF,
						DWORD   dwOldBuffersCount,
						struct sockaddr * lpFrom,
						LPINT			lpFromlen,
						LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
						);
	//##ModelId=44C4B5D200EC
   int FindOverlapped(LPWSAOVERLAPPED pNewOverlapped);
	//##ModelId=44C4B5D200EE
   bool FindOverlapped(LPWSAOVERLAPPED pNewOverlapped,OVERLAPPEDSTRUCT &myOverlapped);
	//##ModelId=44C4B5D200F1
   void DeleteOverlapped(SOCKET hSocket);
	//##ModelId=44C4B5D200FB
   void Destroy();
private:
	//##ModelId=44C4B5D200FC
   int FindOverlapped(SOCKET hSocket);  
	//##ModelId=44C4B5D200FE
   void DeleteOverlapped(int iIndex);

	//##ModelId=44C4B5D2010A
	OVERLAPPEDSTRUCT m_OverlappedStruct[MAX_OVERLAPPED_COUNT];
	//##ModelId=44C4B5D2010E
	int m_iOverlappedCount;
	//##ModelId=44C4B5D2011A
	CLockCS          m_Lock;

};
#endif//__JOYOVERLAPPEDMANAGER_H
